<?php
/*
Copyright (C) 2011 Tony Bruess

This file is a part of MineLink.

MineLink is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

MineLink is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with MineLink. If not, see <http://www.gnu.org/licenses/>.
*/

class MinelinkComponent extends Object
{
	var $socket;
	var $running;
	var $pid;
	var $minecraft;
	var $minelink;
	
	function initialize(&$controller, $settings = array()) {
		// saving the controller reference for later use
		$this->controller =& $controller;
	}
	
	function createSocket()
	{
		$this->minecraft['memorylimit'] = '512';
		$this->minelink['server'] = Configure::read('bukkitadmin.TcpBukkitServer');
		$this->minelink['port'] = Configure::read('bukkitadmin.TcpBukkitPort');
		$this->minelink['pass'] = Configure::read('bukkitadmin.TcpBukkitPassword');
		$this->socket = stream_socket_client($this->minelink['server'] . ':' . $this->minelink['port'], $erno, $erst, 5);
		if(!$this->socket)
		{
			$this->running = false;
			return FALSE;			
		}
		else
		{
			$this->cmd('pass '.$this->minelink['pass']);
			$this->running = true;
			return TRUE;
		}
	}
	
	//called after Controller::beforeFilter()
	function startup(&$controller) {
	}
	
	//called after Controller::beforeRender()
	function beforeRender(&$controller) {
	}
	
	//called after Controller::render()
	function shutdown(&$controller) {
	}
	
	//called before Controller::redirect()
	function beforeRedirect(&$controller, $url, $status=null, $exit=true) {
	}
	
	private function cmd($cmd,$log=false)
	{
		$return="";
		fwrite($this->socket, $cmd . "\n");
		$read = array($this->socket);
		$write = array();
		$except = array();
		while(stream_select($read, $write, $except, 1, 1000))
			$return .= fgets($read[0]);
		if ($log)
			file_put_contents('c:\temp\bukkit.log','cmd : '.$cmd.' result : '.$return,FILE_APPEND);
		$string = explode("\n", $return);
		return json_decode($string[0], true);
	}
	
	public function player_count()
	{
		$data = $this->cmd('playercount');
		return $data['message'];
	}
	
	public function get_inventory($player)
	{
		if(!empty($player))
		{
			$result = $this->cmd('invprobe '.$player);
			return $result["inventory_items"];
		}
	}
	
	public function max_players()
	{
		$data = $this->cmd('maxplayers');
		return $data['message'];
	}
	
	public function players()
	{
		$data = $this->cmd('getplayers');
		return $data;
	}
	
	public function server_status()
	{
		return $this->running;
	}
	
	public function pid()
	{
		$pid = $this->cmd('pid');
		$this->pid = $pid['message'];
		return $pid['message'];
	}
	
	public function cpu_usage()
	{
		if (!stristr(PHP_OS, 'WIN'))
		{
			$return = explode("\n", shell_exec('ps -p'. $this->pid .' -o %cpu'));
			if(!$return[1]) $return[1] = '0';
			return $return[1]."%";
		}
		else
		{
			return 'Linux only';
		}
	}
	
	public function mem_usage()
	{
		if (!stristr(PHP_OS, 'WIN'))
		{
			$return = explode("\n", shell_exec('ps -p'. $this->pid .' -o rss'));
			return number_format(($return[1] / 1024) / $this->minecraft['memorylimit'] * 100,2)."%";
		}
		else
		{
			return 'Linux only';
		}
	}
	
	public function pm_player($player,$message)
	{
	$message = $message;
	if(empty($player))
		{
			return $this->cmd('msg '.$message);
		}
	else
		{
			return $this->cmd('pm '.$player.' '.$message);
		}
	}
	
	public function kick_player($player)
	{
		return $this->cmd('kick '.$player);
	}
	
	public function invadd($player,$block_type,$amount)
	{
		if (is_numeric($amount))
		{
			return $this->cmd('invadd '.$player.' '.$block_type.' '.$amount);
		}
	}
	
	public function invremove($player,$slot)
	{
		return $this->cmd('invremove '.$player.' '.$slot);
	}

}

?>